package com.yneusoft.pctask.aop;

import com.yneusoft.common.tool.DataProcessing;
import com.yneusoft.dao.entity.Staff;
import com.yneusoft.dao.entity.StaffState;
import com.yneusoft.dao.mapper.StaffMapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description： 员工管理切面
 * @Author: zwl
 * @Date: 2021/11/26 16:00
 */
@Component
@Aspect
public class StaffAop {

    @Autowired
    private StaffMapper staffMapper;

    //在insert方法执行之前执行该方法
    @Before(value = "execution(* com.yneusoft.dao.mapper.StaffMapper.insert(..))")
    public void insertStaff(JoinPoint joinPoint){
        //获取参数中的对象数组
        Object[] args = joinPoint.getArgs();
        Staff newStaff = (Staff) args[0];
        //新增员工默认在职
        newStaff.setStateId(1);
        //从缓存里面获取状态名称
        newStaff.setStateName(StaffState.dataMap.get(newStaff.getStateId()).getName());

        //确定该员工是数据库的第几个员工并生成工号
        Integer count = (staffMapper.selectCount(null)) + 1;
        String jobNumber = DataProcessing.GenerateNumber(count);
        newStaff.setJobNumber(jobNumber);
    }
}
